#include "../status_task.h"
#include "./draw_task.h"
#include "./make_ice_task.h"
#include "../../driver/ice/compr.h"
#include "../../driver/ice/ntc_sensor.h"
#include "../../driver/ice/parts.h"
#include "../../driver/ice/stir_motor.h"
#include "../../driver/ice/sensor.h"
#include "../../service/salve_conn.h"

namespace task {

using namespace driver;
using namespace service;

static constexpr uint16_t APP_VERSION = 0x02;

void StatusTask::run() {
    
    for (;;) {
        ntcSensor->read_all();

        uint8_t sw = sensor->is_ice_full() |
            (sensor->is_bucket_high() << 1) |
            (sensor->is_bucket_low() << 2) |
            (sensor->is_leak() << 3) |
            (ntcSensor->is_env_ok() << 4) |
            (ntcSensor->is_evap_ok() << 5);

        uint8_t ctrl = compr->is_open() |
            (parts->is_fan_open() << 1) |
            (parts->is_valve_open() << 2) |
            (parts->is_drop_ice_open() << 3) |
            (parts->is_pump_open() << 4) |
            (stirMotor->is_open() << 5);

        salveConn->status_notify(
            APP_VERSION,
            sw,
            sensor->stir_speed(),
            ntcSensor->env_temp(),
            ntcSensor->evap_temp(),
            ctrl,
            drawTask->state(),
            makeIceTask->state()
        );

        osDelay(500);
    }
}

}
